//
//  MTInfoVerificationTableFooterView.swift
//  MT
//
//  Created by M on 2019/12/23.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

@objc
protocol MTInfoVerificationTableFooterViewDelegate: NSObjectProtocol {
    /// 点击按钮
    @objc optional func tableFooterViewDidClickButton(_ footerView: MTInfoVerificationTableFooterView)
    
    @objc optional func userAgreement()
    
    @objc optional func privacy()
}

class MTInfoVerificationTableFooterView: UIView {
    
    weak var delegate: MTInfoVerificationTableFooterViewDelegate?
    
    var buttonStr: String  {
        didSet {
            button.setTitle(buttonStr, for: .normal)
        }
    }
    
    var needShowRule: Bool = false {
        didSet {
            if needShowRule {
                agreeButton.isHidden = false
                richTitleLabel.isHidden = false
            }
        }
    }

    /// 下一步按钮
    lazy var button: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.theme.main
        btn.setTitle(buttonStr, for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = PingFangSCSemiboldFont(16)
        btn.adjustsImageWhenHighlighted = false
        btn.layer.cornerRadius = 22
        btn.addTarget(self, action: #selector(tableFooterView(didClickButton:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var richTitleLabel: YYLabel = {
        let _richTitleLabel: YYLabel = YYLabel(frame: CGRect.zero)
        _richTitleLabel.font = UIFont.systemFont(ofSize: 10.0, weight: .regular)
        _richTitleLabel.textColor = UIColor.text.x9b9b9b
        _richTitleLabel.textAlignment = .left
        _richTitleLabel.numberOfLines = 0
        return _richTitleLabel
    }()
    
    lazy var agreeButton: UIButton = {
        let _agreeButton: UIButton = UIButton(frame: CGRect.zero)
        _agreeButton.setImage(UIImage.init(named: "noselect"), for: UIControl.State.normal)
        _agreeButton.setImage(UIImage.init(named: "select-yellow"), for: UIControl.State.selected)
        _agreeButton.setTitleColor(UIColor.text.x9b9b9b, for: UIControl.State.normal)
        _agreeButton.addTarget(self, action: #selector(agree(sender:)), for: UIControl.Event.touchUpInside)
        _agreeButton.titleLabel?.font = UIFont.systemFont(ofSize: 12.0)
        _agreeButton.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        _agreeButton.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 14)
        _agreeButton.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 14)
        _agreeButton.isSelected = true
        return _agreeButton
    }()
    
    override init(frame: CGRect) {
        buttonStr = "下一步"
        super.init(frame: frame)
        agreeButton.isHidden = true
        richTitleLabel.isHidden = true
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initializeUI() {
        addSubview(button)
        addSubview(richTitleLabel)
        addSubview(agreeButton)
        button.snp.makeConstraints { (make) in
            make.bottom.equalTo(self)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(44)
        }
        
        agreeButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(28)
            make.top.equalTo(button.snp.top).offset(-60)
            make.centerX.equalTo(self).offset(-110)
        }
        
        richTitleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(agreeButton)
            make.left.equalTo(agreeButton.snp.right).offset(-10)
        }
        
        setRichLabel()
    }
    
    func setRichLabel() {
        //设置整段字符串的颜色
        let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key(rawValue: "NSFontAttributeName"): UIFont.systemFont(ofSize: 10.0, weight: .regular), NSAttributedString.Key(rawValue: "NSForegroundColorAttributeName"): UIColor.text.x9b9b9b]
        
        let attributedText: NSMutableAttributedString = NSMutableAttributedString.init(string: "我已阅读并同意《用户协议》及《隐私政策》", attributes: attributes)
        //设置高亮色和点击事件
        attributedText.setTextHighlight(attributedText.string.nsrange(of: "《用户协议》")[0], color: UIColor.init(hex: 0x426bf2), backgroundColor: nil, tapAction: { (containerView, text, range, rect) in
            //            let ecommController: DDCEcommerceWebViewController = DDCEcommerceWebViewController.init(urlString: String.init(format: "%@%@", (DDCStore.shared()?.base_h5_url)!,userAgreement), title: "用户协议")
            //            self.viewController?.navigationController?.pushViewController(ecommController, animated: true)
            self.delegate?.userAgreement!()
        }, longPressAction: { (view, text, range, rect) in
            
        })
        //        //设置高亮色和点击事件
        attributedText.setTextHighlight(attributedText.string.nsrange(of: "《隐私政策》")[0], color: UIColor.init(hex: 0x426bf2), backgroundColor: nil, tapAction: { (containerView, text, range, rect) in
            //            let ecommController: DDCEcommerceWebViewController = DDCEcommerceWebViewController.init(urlString:        String.init(format: "%@%@", (DDCStore.shared()?.base_h5_url)!,privacyPolicy) , title: "隐私政策")
            //            self.viewController?.navigationController?.pushViewController(ecommController, animated: true)
            self.delegate?.privacy!()
        }, longPressAction: { (view, text, range, rect) in
            
        })
        
//        attributedText.setUnderlineStyle(.single, range: attributedText.string.nsrange(of: "《用户协议》")[0])
//        attributedText.setUnderlineStyle(.single, range: attributedText.string.nsrange(of: "《隐私政策》")[0])
//
//        attributedText.setUnderlineColor(.red, range: attributedText.string.nsrange(of: "《用户协议》")[0])
//        attributedText.setUnderlineColor(.red, range: attributedText.string.nsrange(of: "《隐私政策》")[0])
        
        self.richTitleLabel.attributedText = attributedText
    }
    
    @objc
    private func tableFooterView(didClickButton sender: UIButton) {
        if let delegate = delegate {
            delegate.tableFooterViewDidClickButton?(self)
        }
    }
    
    @objc func agree(sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    
}
